package me.like.diandian.base;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.FragmentActivity;

import com.gyf.barlibrary.ImmersionBar;
import com.umeng.analytics.MobclickAgent;

import java.util.concurrent.TimeUnit;

import io.reactivex.disposables.CompositeDisposable;
import me.like.diandian.util.ActivityUtil;
import me.like.diandian.view.ToastView;

import static com.jakewharton.rxbinding2.view.RxView.clicks;

/**
 * Created by shousakai on 2019/6/20.
 */

public abstract class BaseActivity<T extends ViewDataBinding> extends FragmentActivity implements BaseContract.BaseView {
    protected T binding;
    public CompositeDisposable mCompositeSubscription;
    public ImmersionBar mImmersionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, getLayoutId());
        mCompositeSubscription = new CompositeDisposable();
        ActivityUtil.getActivityList().add(this);
        //初始化沉浸式
        if (isImmersionBarEnabled()) {
            initImmersionBar();
        }


    }

    protected abstract int getLayoutId();


    public void onClick(View view, io.reactivex.functions.Consumer consumer) {
        mCompositeSubscription.add(clicks(view).throttleFirst(500, TimeUnit.MILLISECONDS).subscribe(consumer));
    }

    /**
     * 是否可以使用沉浸式
     * Is immersion bar enabled boolean.
     *
     * @return the boolean
     */
    protected boolean isImmersionBarEnabled() {
        return true;
    }

    protected void initImmersionBar() {
        //在BaseActivity里初始化
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.init();
    }


    @Override
    public void showError(String err) {
        ToastView.initToast().textToast(this, err);
    }

    @Override
    public void complete() {

    }

    protected void setstateBar(boolean black) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //获取窗口区域
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            if (colorID == R.color.color_ffffff) {
//设置状态栏颜色
//                window.setStatusBarColor(UIUtils.getColor(colorID));

//设置显示为白色背景，黑色字体
            if (black) {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            } else {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (ActivityUtil.getActivityList().contains(this)) {
            ActivityUtil.getActivityList().remove(this);
        }
        if (mCompositeSubscription != null && !mCompositeSubscription.isDisposed()) {
            mCompositeSubscription.dispose();
            mCompositeSubscription.clear();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
